package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.dao.ProductAttributeDao;
import com.wing.product.model.entity.ProductAttribute;
import com.wing.product.model.entity.ProductAttributeValue;
import com.wing.product.model.request.ProductAttributeForm;
import com.wing.product.model.request.ProductAttributeValueForm;
import com.wing.product.model.response.ProductAttributeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 产品类型属性-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeDao, ProductAttribute> implements ProductAttributeService {

    @Autowired
    private ProductAttributeValueService productAttributeValueService;

    @Override
    @Transactional
    public int add(ProductAttributeForm productAttributeForm) {

        //校验

        ProductAttribute productAttribute =new ProductAttribute();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductAttribute.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productAttributeForm, productAttribute, copyOptions);

        int insertCount=baseMapper.insert(productAttribute);
        productAttributeForm.setId(productAttribute.getId());
        saveDetail(productAttributeForm);
        return insertCount;
    }

    void saveDetail(ProductAttributeForm productAttributeForm){
        if(CollectionUtils.isNotEmpty(productAttributeForm.getDetailList())){
            for (ProductAttributeValueForm attributeValueForm:
                 productAttributeForm.getDetailList()) {
                ProductAttributeValue productAttributeValue=new ProductAttributeValue();
                BeanUtils.copyProperties(attributeValueForm,productAttributeValue);
                productAttributeValue.setProductAttributeId(productAttributeForm.getId());
                productAttributeValueService.save(productAttributeValue);
            }
        }
    }

    @Override
    public List<ProductAttributeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductAttributeVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductAttributeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductAttributeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int update(ProductAttributeForm productAttributeForm) {
        ProductAttribute productAttribute=new ProductAttribute();
        BeanUtils.copyProperties(productAttributeForm,productAttribute);
        int updateCount=baseMapper.updateById(productAttribute);
        productAttributeValueService.remove(new QueryWrapper<ProductAttributeValue>().eq("product_attribute_id",productAttributeForm.getId()));
        saveDetail(productAttributeForm);
        return updateCount;
    }
}
